package edu.scau.divineplace.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import edu.scau.divineplace.entity.dos.*;
import edu.scau.divineplace.entity.enums.LikeTargetTypeEnum;
import edu.scau.divineplace.entity.query.PageQuery;
import edu.scau.divineplace.entity.vo.JournalVO;
import edu.scau.divineplace.entity.vo.content.ContentSimplyInfoVO;
import edu.scau.divineplace.mapper.JournalServiceMapper;
import edu.scau.divineplace.util.BeanConvertUtils;
import edu.scau.divineplace.util.PageQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: x1aolone
 * @Date: 2020/12/11 15:48
 */
@Service
public class JouanlService {

    @Autowired
    private ImportMapperService importMapperService;

    @Autowired
    private JournalServiceMapper journalServiceMapper;

    @Autowired
    private LikeService likeService;

    /**
     * 通过内容id获取日志列表
     * @param contentId
     * @return
     */
    public List<JournalVO> list(PageQuery pageQuery, Long contentId){
        PageQueryUtil.fillAllIfAbsent(pageQuery);
        List<JournalVO> VOS = Lists.newArrayList();
        Page<JournalDO> journalPage = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        LambdaQueryWrapper<JournalDO> query = Wrappers.<JournalDO>lambdaQuery()
                .eq(JournalDO::getContentId, contentId)
                .orderByDesc(JournalDO::getUpdateTime)
                .orderByDesc(JournalDO::getCreateTime);
        journalPage = importMapperService.journalMapper.selectPage(journalPage, query);
        for (JournalDO DO : journalPage.getRecords()) {
            final Long journalId = DO.getId();
            final Long userId = DO.getUserId();
            // 获取发表用户信息
            UserDO UDO = importMapperService.userMapper.selectById(userId);
            // 获取点赞数
            Integer likeNum = likeService.count(LikeTargetTypeEnum.JOURNAL, journalId);
            VOS.add(
                    JournalVO.builder()
                            .journal(DO)
                            .user(UDO)
                            .likeNum(likeNum)
                            .build()
            );
        }
        return VOS;
    }

    /**
     * 通过日志id获取日志信息
     * @param journalId
     * @return
     */
    public JournalVO get(Long journalId){
        Preconditions.checkNotNull(journalId);
        JournalDO JDO = importMapperService.journalMapper.selectById(journalId);
        Preconditions.checkNotNull(JDO, "日志不存在");
        UserDO UDO = importMapperService.userMapper.selectById(JDO.getUserId());
        ContentBasicInformationDO CDO = importMapperService.contentBasicInformationMapper.selectById(JDO.getContentId());
        // 获取点赞数
        Integer likeNum = likeService.count(LikeTargetTypeEnum.JOURNAL, journalId);
        JournalVO VO = JournalVO.builder()
                .journal(JDO)
                .user(UDO)
                .contentInfo(CDO)
                .likeNum(likeNum)
                .build();
        return VO;
    }

    /**
     * 通过内容id、用户id获取用户发表的日志
     * @param contentId
     * @param userId
     * @return
     */
    public JournalVO get(Long contentId, Long userId){
        LambdaQueryWrapper<JournalDO> query = Wrappers.<JournalDO>lambdaQuery()
                .eq(JournalDO::getContentId, contentId)
                .eq(JournalDO::getUserId, userId)
                .last("limit 1");
        JournalDO JDO = importMapperService.journalMapper.selectOne(query);
        JournalVO VO = JournalVO.builder()
                .journal(JDO)
                .build();
        return VO;
    }

    /**
     * 通过内容类型获取该类型的内容的最近评论，支持分页
     * @param pageQuery
     * @param contentType
     * @return
     */
    public List<Map<String, Object>> listRecentJournal(PageQuery pageQuery, Integer contentType){
        PageQueryUtil.fillAllIfAbsent(pageQuery);
        Page<Map<String, Object>> contentPage = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        List<Map<String, Object>> VOS = journalServiceMapper.selectRecentJournal(contentPage, contentType).getRecords();
        return VOS;
    }

    /**
     * 通过内容id、用户id、日志标题、日志内容，新增日志
     * @param contentId
     * @param userId
     * @param title
     * @param comment
     */
    public void add (Long contentId, Long userId, String title, String comment) {
        JournalDO DO = new JournalDO();
        DO.setContentId(contentId);
        DO.setUserId(userId);
        DO.setTitle(title);
        DO.setComment(comment);
        importMapperService.journalMapper.insert(DO);
    }

    /**
     * 通过内容id、用户id、分数、评论，修改评论
     * @param contentId
     * @param userId
     * @param title
     * @param comment
     */
    public void update (Long contentId, Long userId, String title, String comment) {
        LambdaUpdateWrapper<JournalDO> updateWrapper = Wrappers.<JournalDO>lambdaUpdate()
                .eq(JournalDO::getContentId, contentId)
                .eq(JournalDO::getUserId, userId)
                .set(JournalDO::getTitle, title)
                .set(JournalDO::getComment, comment);
        // entity传null的时候，自动填充的updateTime不会填充进去，所以new一个
        importMapperService.journalMapper.update(new JournalDO(), updateWrapper);
    }

    /**
     * 通过评分id、分数、评论，修改评论
     * @param journalId
     * @param title
     * @param comment
     */
    public void update (Long journalId, String title, String comment) {
        LambdaUpdateWrapper<JournalDO> updateWrapper = Wrappers.<JournalDO>lambdaUpdate()
                .eq(JournalDO::getId, journalId)
                .set(JournalDO::getTitle, title)
                .set(JournalDO::getComment, comment);
        // entity传null的时候，自动填充的updateTime不会填充进去，所以new一个
        importMapperService.journalMapper.update(new JournalDO(), updateWrapper);
    }


}
